#!/usr/bin/env python
# -*- coding:utf-8 -*-

# file:do_exam1.py
# author:王雯雯
# datetime:2024-11-12 
# software: PyCharm

class StudentScoreManager:
    def __init__(self, file_path):

        self.file_path = file_path

    def add_student(self, class_name, student_id, name, python_score):

        with open(self.file_path, "a") as f:
            f.write(f"{class_name},{student_id},{name},{python_score}\n")

    def delete_student(self, student_id):

        students = self.read_students()
        with open(self.file_path, "w") as f:
            for student in students:
                if student[1]!= student_id:
                    f.write(f"{student[0]},{student[1]},{student[2]},{student[3]}\n")

    def update_student(self, student_id, new_python_score):

        students = self.read_students()
        with open(self.file_path, "w") as f:
            for student in students:
                if student[1] == student_id:
                    student[3] = new_python_score
                f.write(f"{student[0]},{student[1]},{student[2]},{student[3]}\n")

    def search_student(self, student_id):

        students = self.read_students()
        for student in students:
            if student[1] == student_id:
                return student
        return None

    def read_students(self):

        students = []
        with open(self.file_path, "r") as f:
            for line in f.readlines():
                student_info = line.strip().split(",")
                students.append(student_info)
        return students
manager = StudentScoreManager("students.txt")
manager.add_student("一班", "001", "赵六", 90)
manager.add_student("二班", "002", "孙七", 85)
print(manager.search_student("001"))
manager.update_student("002", 92)
manager.delete_student("001")